﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace homeIMSClient
{
    public partial class logReg : Form
    {
        //public variables of user object for transition to main form and service variable to use for session
        public homeIMSRef.userInfo emptyUser;
        public homeIMSRef.IhomeIMSService service = new homeIMSRef.IhomeIMSServiceClient();
        public logReg()
        {
            InitializeComponent();
            //instantiate and fill public variable
            emptyUser = new homeIMSRef.userInfo();
        }

        //action will connect to service and retrieve user
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //run method
            Login(txtUserName.Text, txtPassword.Text);
        }    

        //Action will connect and create user entry in database
        private void btnRegister_Click(object sender, EventArgs e)
        {
            //check for empty password box since userName has already been checked
            if (txtRpassword.Text == "" | txtRpassword.Text == null)
            {
                //change cursor back
                Cursor.Current = Cursors.Default;

                //message box
                MessageBox.Show("Please provide a password");
            }
            else
            {
                //change cursor
                Cursor.Current = Cursors.WaitCursor;
                //create userInfo object
                homeIMSRef.userInfo newUser = new homeIMSRef.userInfo();

                //fill object with information
                newUser.userName = txtRuserName.Text;
                newUser.password = txtRpassword.Text;
                newUser.address = txtRaddress.Text;
                newUser.city = txtRcity.Text;
                newUser.state = txtRstate.Text;
                newUser.zip = txtRzip.Text;
                newUser.phoneNumber = txtRphoneNum.Text;

                //create int variable to check result of method
                int result = service.register(newUser);

                //check result of successful insert
                switch (result)
                {
                    case 1:
                        {
                            //if successful, send message box for success and login
                            MessageBox.Show("User Registered.");
                            Login(newUser.userName, newUser.password);
                        }
                        break;

                    case 0:
                        {
                            //if false, send message box to try again
                            MessageBox.Show("Register failed. Please try again");
                        }
                        break;
                }
                Cursor.Current = Cursors.Default;
                
            }
        }

        //method will login user when button is clicked
        private void Login(string userName, string password)
        {

            //check to see if input is filled
            if (userName != "" && userName != null && password != "" && password != null)
            {
                //set cursor to wait
                Cursor.Current = Cursors.WaitCursor;
                try
                {
                    homeIMSRef.userInfo newUserInfo = service.Login(userName, password);

                    //check to see if object is filled
                    if (newUserInfo.userID != 0)
                    {
                        //if it is filled, store in variable
                        emptyUser = newUserInfo;

                        //set cursor back
                        Cursor.Current = Cursors.Default;

                        //close form
                        this.Close();
                    }

                    else
                    {
                        //set cursor back to default
                        Cursor.Current = Cursors.Default;

                        //if user is empty or contains invalid information, send error
                        MessageBox.Show("Record not found.....login failed");
                    }
                }

                catch (NullReferenceException)
                {
                    //if null reference, pop up message box
                    MessageBox.Show("please try again.....login failed");
                }
                catch (Exception ex)
                {
                    //if any other exception occurs, pop up message w/ exception
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                //set cursor back to default
                Cursor.Current = Cursors.Default;

                //if empty or null, messagebox to try again
                MessageBox.Show("Username or Password is required");
            }
        }

        //if cancel button is hit, exit application.
        private void btnCancel_Click(object sender, EventArgs e)
        {
            //if cancel button clicked, set emptyUser to null;
            Environment.Exit(0);
        }

        //Action will validate the Username to see if it is available
        private void txtRuserName_Leave(object sender, EventArgs e)
        {
            //change cursor to wait and lock form
            Cursor.Current = Cursors.WaitCursor;

            //create result bool to check for result with default value of false
            bool result = false;

            //check to make sure txtbox is filled
            if (txtRuserName.Text == "" || txtRuserName.Text == null)
            {
                //change cursor back
                Cursor.Current = Cursors.Default;

                //disable button
                btnRegister.Enabled = false;
            }
            else
            {
                //pass information to service
                result = service.CheckUserName(txtRuserName.Text.ToString());

                //check result
                switch (result)
                {
                    case true:
                        {
                            //if true, allow continuation of register
                            Cursor.Current = Cursors.Default;

                            //show message box of success
                            MessageBox.Show("UserName accepted. Please continue with registering");

                            //enable Register Button
                            btnRegister.Enabled = true;
                        }
                        break;
                    case false:
                        {
                            //if false, allow user to correct themselves
                            Cursor.Current = Cursors.Default;

                            //show message box of failure
                            MessageBox.Show("UserName not accepted or in use. Please try again with a different UserName");
                            btnRegister.Enabled = false;
                        }
                        break;
                }
            }
        }
    }
}
